﻿/**********************************************************************************
 * 程序说明：     数据库助手类(简单型无参数)
 * 创建日期：     2009.10.20
 * 程序制作：     agui
 * 联系方式：     mailto: 35499039 3@qq.com  
 * ********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using PlugNT.Common;
using PlugNT.Safe;

namespace PlugNT.Database.Common.Simple
{
    /// <summary>
    /// 数据库帮助类（需要修改，重载Complex即可）
    /// </summary>
    public abstract class DbHelper
    {
        /// <summary>
        /// 数据库的信息类
        /// </summary>
        public readonly static DbInfo dbinfo = new DbInfo(IConfig.DbType, IConfig.ConnectionString);  //只初始化一次

        #region 公用方法
        /// <summary>
        /// 判断是否存在某表的某个字段(Access和Mysql不能用)
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="columnName">列名称</param>
        /// <returns>是否存在</returns>
        public static bool ExistsColumn(string tableName, string columnName)
        {
            return Complex.DbHelper.ExistsColumn(dbinfo, tableName, columnName);
        }

        public static int GetMaxField(string FieldName, string TableName)
        {
            return Complex.DbHelper.GetMaxField(dbinfo, FieldName, TableName);
        }
        /// <summary>
        /// 表是否存在(Mysql不能用)
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static bool ExistsTable(string TableName)
        {
            return Complex.DbHelper.ExistsTable(dbinfo, TableName);
        }
        public static bool Exists(string strSql)
        {
            return Complex.DbHelper.Exists(dbinfo, strSql);
        }
        public static bool Exists(string strSql, params DbParameter[] cmdParms)
        {
            return Complex.DbHelper.Exists(dbinfo, strSql, cmdParms);
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString)
        {
            return Complex.DbHelper.GetSingle(dbinfo, SQLString);
        }
        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString, params DbParameter[] cmdParms)
        {
            return Complex.DbHelper.GetSingle(dbinfo, SQLString, cmdParms);
        }
        /// <summary>
        /// 重置表
        /// </summary>
        /// <param name="dbinfo"></param>
        /// <param name="tabName"></param>
        /// <param name="idName"></param>
        /// <returns></returns>
        public static bool ReSetTable(string tabName, string idName)
        {
            return Complex.DbHelper.ReSetTable(dbinfo, tabName, idName);
        }
        #endregion

        #region  执行简单SQL语句
        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            return Complex.DbHelper.ExecuteSql(dbinfo, SQLString);
        }
        public static int ExecuteSqlByTime(string SQLString, int Times)
        {
            return Complex.DbHelper.ExecuteSqlByTime(dbinfo, SQLString, Times);
        }
        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>		
        public static int ExecuteSqlTran(List<String> SQLStringList,out string error)
        {
            return Complex.DbHelper.ExecuteSqlTran(dbinfo, SQLStringList, out error);
        }

        /// <summary>
        /// 执行查询语句，返回DbDataReader ( 注意：调用该方法后，一定要对DbDataReader进行Close )
        /// </summary>
        /// <param name="SQLString"></param>
        /// <returns></returns>
        public static DbDataReader ExecuteReader(string SQLString)
        {
            return Complex.DbHelper.ExecuteReader(dbinfo, SQLString);

        }
        /// <summary>
        /// 执行查询语句，返回DataTable
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataTable</returns>
        public static DataTable GetDataTable(string SQLString)
        {
            return GetDataSet(SQLString).Tables[0];
        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSet(string SQLString)
        {
            return Complex.DbHelper.GetDataSet(dbinfo, SQLString);
        }
        #endregion

        #region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params DbParameter[] cmdParms)
        {
            return Complex.DbHelper.ExecuteSql(dbinfo, SQLString, cmdParms);
        }

        /// <summary>
        /// 执行查询语句，返回DbDataReader ( 注意：调用该方法后，一定要对DbDataReader进行Close )
        /// </summary>
        /// <param name="SQLString"></param>
        /// <param name="cmdParms"></param>
        /// <returns></returns>
        public static DbDataReader ExecuteReader(string SQLString, params DbParameter[] cmdParms)
        {
            return Complex.DbHelper.ExecuteReader(dbinfo, SQLString, cmdParms);
        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet GetDataSet(string SQLString, params DbParameter[] cmdParms)
        {
            return Complex.DbHelper.GetDataSet(dbinfo, SQLString, cmdParms);
        }

        #endregion

        #region 存储过程操作（限于sql server）

        /// <summary>
        /// 执行存储过程，返回SqlDataReader ( 注意：调用该方法后，一定要对SqlDataReader进行Close )
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlDataReader</returns>
        public static DbDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
        {
            return Complex.DbHelper.RunProcedure(dbinfo, storedProcName, parameters);
        }


        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="tableName">DataSet结果中的表名</param>
        /// <returns>DataSet</returns>
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
        {
            return Complex.DbHelper.RunProcedure(dbinfo, storedProcName, parameters, tableName);
        }

        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
        {
            return Complex.DbHelper.RunProcedure(dbinfo, storedProcName, parameters, tableName, Times);
        }



        /// <summary>
        /// 执行存储过程，返回影响的行数		
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            return Complex.DbHelper.RunProcedure(dbinfo, storedProcName, parameters, out rowsAffected);
        }

        #endregion

        #region contact me
        public static string Help()
        {
            return @"mailto:354990393@qq.com";
        }
        #endregion
    }
}
